package chapt05.ccube;
// CCube.java
// OpenGL SuperBible
// Demonstrates primitive RGB Color Cube
// Program by Richard S. Wright Jr.

import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;

public class CCube
{
	static GLU glu = new GLU();
	
	// Rotation amounts
	static float xRot = 0.0f;
	static float yRot = 0.0f;
	
	// This function does any needed initialization on the rendering context.
	protected static void setup(GL2 gl2, int width, int height)
	{
		// Black background
		gl2.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		
		gl2.glEnable(GL2.GL_DEPTH_TEST);
		gl2.glEnable(GL2.GL_DITHER);
		gl2.glShadeModel(GL2.GL_SMOOTH);
	}
	
	// Called to draw scene
	protected static void render(GL2 gl2, int w, int h)
	{
		float fAspect;
		
		// Prevent a divide by zero
		if(h == 0)
			h = 1;
		
		// Set Viewport to window dimensions
		gl2.glViewport(0, 0, w, h);
		
		// Reset coordinate system
		gl2.glMatrixMode(GL2.GL_PROJECTION);
		gl2.glLoadIdentity();
		
		fAspect = (float)w / (float)h;
		glu.gluPerspective(35.0f, fAspect, 1.0f, 1000.0f);
		
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glLoadIdentity();
		gl2.glTranslatef(0.0f, 0.0f, -400.0f);
		
		// Clear the window with current clearing color
		gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
		
		gl2.glPushMatrix();
		
		gl2.glRotatef(xRot, 1.0f, 0.0f, 0.0f);
		gl2.glRotatef(yRot, 0.0f, 1.0f, 0.0f);
		
		// Draw six quads
		gl2.glBegin(GL2.GL_QUADS);
			// Front Face
			// White
			gl2.glColor3ub((byte)255, (byte)255, (byte)255);
			gl2.glVertex3f(50.0f, 50.0f, 50.0f);
			
			// Yellow
			gl2.glColor3ub((byte)255, (byte)255, (byte)0);
			gl2.glVertex3f(50.0f, -50.0f, 50.0f);
			
			// Red
			gl2.glColor3ub((byte)255, (byte)0, (byte)0);
			gl2.glVertex3f(-50.0f, -50.0f, 50.0f);
			
			// Magenta
			gl2.glColor3ub((byte)255, (byte)0, (byte)255);
			gl2.glVertex3f(-50.0f, 50.0f, 50.0f);
			
		// Back Face
			// Cyan
			gl2.glColor3f(0.0f, 1.0f, 1.0f);
			gl2.glVertex3f(50.0f, 50.0f, -50.0f);
			
			// Green
			gl2.glColor3f(0.0f, 1.0f, 0.0f);
			gl2.glVertex3f(50.0f, -50.0f, -50.0f);
			
			// Black
			gl2.glColor3f(0.0f, 0.0f, 0.0f);
			gl2.glVertex3f(-50.0f, -50.0f, -50.0f);
			
			// Blue
			gl2.glColor3f(0.0f, 0.0f, 1.0f);
			gl2.glVertex3f(-50.0f, 50.0f, -50.0f);
			
		// Top Face
			// Cyan
			gl2.glColor3f(0.0f, 1.0f, 1.0f);
			gl2.glVertex3f(50.0f, 50.0f, -50.0f);
			
			// White
			gl2.glColor3f(1.0f, 1.0f, 1.0f);
			gl2.glVertex3f(50.0f, 50.0f, 50.0f);
			
			// Magenta
			gl2.glColor3f(1.0f, 0.0f, 1.0f);
			gl2.glVertex3f(-50.0f, 50.0f, 50.0f);
			
			// Blue
			gl2.glColor3f(0.0f, 0.0f, 1.0f);
			gl2.glVertex3f(-50.0f, 50.0f, -50.0f);
			
		// Bottom Face
			// Green
			gl2.glColor3f(0.0f, 1.0f, 0.0f);
			gl2.glVertex3f(50.0f, -50.0f, -50.0f);
			
			// Yellow
			gl2.glColor3f(1.0f, 1.0f, 0.0f);
			gl2.glVertex3f(50.0f, -50.0f, 50.0f);
			
			// Red
			gl2.glColor3f(1.0f, 0.0f, 0.0f);
			gl2.glVertex3f(-50.0f, -50.0f, 50.0f);
			
			// Black
			gl2.glColor3f(0.0f, 0.0f, 0.0f);
			gl2.glVertex3f(-50.0f, -50.0f, -50.0f);
			
		// Left face
			// White
			gl2.glColor3f(1.0f, 1.0f, 1.0f);
			gl2.glVertex3f(50.0f, 50.0f, 50.0f);
			
			// Cyan
			gl2.glColor3f(0.0f, 1.0f, 1.0f);
			gl2.glVertex3f(50.0f, 50.0f, -50.0f);
			
			// Green
			gl2.glColor3f(0.0f, 1.0f, 0.0f);
			gl2.glVertex3f(50.0f, -50.0f, -50.0f);
			
			// Yellow
			gl2.glColor3f(1.0f, 1.0f, 0.0f);
			gl2.glVertex3f(50.0f, -50.0f, 50.0f);
			
		// Right face
			// Magenta
			gl2.glColor3f(1.0f, 0.0f, 1.0f);
			gl2.glVertex3f(-50.0f, 50.0f, 50.0f);
			
			// Blue
			gl2.glColor3f(0.0f, 0.0f, 1.0f);
			gl2.glVertex3f(-50.0f, 50.0f, -50.0f);
			
			// Black
			gl2.glColor3f(0.0f, 0.0f, 0.0f);
			gl2.glVertex3f(-50.0f, -50.0f, -50.0f);
			
			// Red
			gl2.glColor3f(1.0f, 0.0f, 0.0f);
			gl2.glVertex3f(-50.0f, -50.0f, 50.0f);
		gl2.glEnd();
		
		gl2.glPopMatrix();
		
		// Show the graphics
		gl2.glFlush();
	}
}